/**
 * @author Stephan Pfaff (http://www.derdaumen.net)
 */

/**
 * The Button Object
 * @param vars object providing the button properties (loaded from the database)
 * @return
 */
function Button(vars){
	if(vars != null) {
		try {
			Item.call(this, vars.prefix, vars.id, vars.type, vars.basename, vars.path, vars.label, vars.alias, vars.thumb, vars.thumb_enable, false, vars.item_class);
			this.category_id 	= parseInt(vars.category_id);
			this.project_id 	= parseInt(vars.project_id);
			if (vars.img != null && vars.img != '')
				this.img 		= siteRoot+vars.img;
			this.img_title		= vars.img_title;
			this.img_enable 	= vars.img_enable;
			this.lightbox_enable = vars.lightbox_enable;
			this.x				= parseInt(vars.x);
			this.y				= parseInt(vars.y);
			this.content_x		= parseInt(vars.content_x);
			this.content_y		= parseInt(vars.content_y);
			this.content_width	= parseInt(vars.content_width);
			this.content_height	= parseInt(vars.content_height);
			this.interactive	= vars.interactive;
			this.text			= vars.text;
			this.text_enable	= vars.text_enable;
			this.link			= vars.link;
			this.link_enable	= vars.link_enable;
			this.img_delete 	= false;
		} catch (e) {
			if(debug) alert('Initialization of Button failed, missing or wrong parameter! ('+e+')');
		}
	} else {
		try {
			var basename = '';
			var path = '';
			var project_id = 0;
			if(getProjects().currentItem() != null) {
				basename 		= getCategories().currentItem().alias + '_'
								+ getProjects().currentItem().alias + '_'
								+ $('#btn_alias').val();
				path 			= getCategories().currentItem().alias + '/'
								+ getProjects().currentItem().alias + '/'
								+ $('#btn_alias').val() + '/';
				project_id = getProjects().currentItem().id;
			} else if(getCategories().currentItem() != null) {
				basename 		= getCategories().currentItem().alias + '_'
								+ $('#btn_alias').val();
				path 			= getCategories().currentItem().alias + '/buttons/'
								+ $('#btn_alias').val() + '/';
				project_id = 0;
			} else {
				alert('Wähle erst ein Element aus!');
			}
			Item.call(this,
					'btn',
					0,
					"button",
					basename,
					path,
					'',
					$('#btn_alias').val(),
					'',
					false,
					false,
					'');
			this.category_id 	= getCategories().currentItem().id;
			this.project_id 	= project_id;
			this.img 			= '';
			this.img_title		= '';
			this.img_enable 	= false;
			this.lightbox_enable = false;
			this.x				= 0;
			this.y				= 0;
			this.content_x		= 0;
			this.content_y		= 0;
			this.content_width	= 1;
			this.content_height	= 1;
			this.interactive	= 1;
			this.text			= '';
			this.text_enable	= false;
			this.link			= '';
			this.link_enable	= false;
			this.img_delete 	= false;
		} catch (e) {
			if(debug) alert('Initialization of new empty Button failed, missing or wrong parameter! ('+e+')');
		}
	}
};
Button.prototype = new Item();
Button.prototype.category_id = 0;
Button.prototype.project_id = 0;
Button.prototype.x = 0;
Button.prototype.y = 0;
Button.prototype.content_x = 0;
Button.prototype.content_y = 0;
Button.prototype.content_width = 1;
Button.prototype.content_height = 1;
Button.prototype.interactive = 0;
Button.prototype.link = "";
Button.prototype.link_enable = false;
Button.prototype.text = "";
Button.prototype.text_enable = false;
Button.prototype.img = "";
Button.prototype.img_title = "";
Button.prototype.img_enable = false;
Button.prototype.lightbox_enable = false;
Button.prototype.img_delete = false;

/**
 * Checks if all the mandatory inputs for a updating/saving a button are correct. 
 */
Button.prototype.check = function() {
	getButtons().hideErrorMessages();
	var isValid = true;
	var btn_alias = $('#btn_alias').val() == '' ? false : true;
	var btn_img = $('#btn_img_path').val() == '' ? false : true;
	var btn_img_enable = $('#btn_img_enable').is(':checked');
	var btn_text = $('#btn_text').val() == '' ? false : true;
	var btn_text_enable = $('#btn_text_enable').is(':checked');
	var btn_link = $('#btn_link').val() == '' ? false : true;
	var btn_link_enable = $('#btn_link_enable').is(':checked');
	if (!btn_alias) {
		isValid = false;
		$('#btn_alias').showError('Alias darf nicht leer sein!');
	}
	/*if (!btn_img && !btn_text && !btn_link) {
		isValid = false;
		$('#btn_content_container').showError('Der Button mus einen Inhalt besitzen (Bild, Text oder Link)!');
		$('#btn_text').showError('Der Button mus einen Inhalt besitzen (Bild, Text oder Link)!');
		$('#btn_link').showError('Der Button mus einen Inhalt besitzen (Bild, Text oder Link)!');
	} else {
		if (btn_img && !btn_img_enable && !btn_text_enable && !btn_link_enable) {
			isValid = false;
			$('#btn_img_enable').showError('Bild nicht aktiviert!');
		}
		if (btn_text && !btn_text_enable && !btn_img_enable && !btn_link_enable) {
			isValid = false;
			$('#btn_text_enable').showError('Text nicht aktiviert!');
		}
		if (btn_link && !btn_link_enable && !btn_img_enable && !btn_text_enable) {
			isValid = false;
			$('#btn_link_enable').showError('Link nicht aktiviert!');
		}
	}*/
	return isValid;
};